mui.ready(function() {

	function getPic () {
        $.ajax({
            type : 'post',
            url : '/api/v1/sms/getUuid',
            data : {
                paramStr : "{'type':'" + 'register' + "','remark':'" + "" + "'}"
            },
            dataType : 'json',
            timeout : 10000,
            success : function (data) {
                var obj = JSON.parse(data.data);
                var picUrl = obj.verifyInfo.picUrl;
                var uuid = obj.verifyInfo.uuid;
                sessionStorage.uuid = uuid;
                sessionStorage.picUrl = picUrl;
                $('#pic').attr('src',sessionStorage.picUrl);
            },
            error : function (xhr,type,errorThrown) {
                // getPic();
            }
        });
    }

	function remind(message){
		
		$('#hidden_box').css('display','block').text(message);
		setTimeout(function(){
			$('#hidden_box').css('display','none');
		},3000);
	}

	new_element = document.createElement("script");
	new_element.setAttribute("type", "text/javascript");
	new_element.setAttribute("src", "lib/des3.js"); // 在这里引入了a.js
	document.body.appendChild(new_element);
	document.querySelector(".agreement").addEventListener("tap", function() {
		mui.openWindow({
			url: '../api/v1/agreement/use',
		});
	});
	var str = window.location.href + '';
	var arr = [];
	arr = str.split("userName");
	if(arr.length > 1) {
		var result = arr[1].substring(0);
	}
	document.querySelector(".pMobile").innerHTML = result;
	//截取字符串
	var smsVerifyCode = document.querySelector(".smsVerifyCode");
	smsVerifyCode.addEventListener("tap", function() {
		var reg_phone = document.querySelector(".reg_phone").value;
		var reg1 = /(^1[3|4|5|7|8][0-9]{9}$)/;
		if(reg1.test(reg_phone) == false) {
			remind('请输入正确的手机号码');
			return
		}
        var verifyCode = document.querySelector('#input_pic_message').value;
        verifyCode = verifyCode == null ? null : verifyCode;
		mui.ajax("/api/v1/sms/send", {
			data: {
				paramStr: DES3.encrypt("{'mobile':'"+ reg_phone + "','type':'" + "register" + "','uuid':'" + sessionStorage.uuid + "','verifyCode':'" + verifyCode +"'}")
			},
			dataType: 'json',
			type: 'post',		
			success: function(data) {
				if(data.code == 200){
					smsVerifyCode.disabled = true;
					var next_time = 60;
					var time_out = setInterval(function() {
						if(next_time == 0) {
							smsVerifyCode.disabled = false;
							smsVerifyCode.innerHTML = "获取验证码";
							clearInterval(time_out);
						} else {
							next_time = next_time - 1;
							smsVerifyCode.innerHTML = next_time;
						}
					}, 1000);
				}else if(data.code == 108){
					$('.testPic').css('display','block');
					getPic();
					$('#pic').click(function(){
		                getPic();
		            });
				}

				$('#hidden_box').css('display','block').text(data.message);
				setTimeout(function(){
					$('#hidden_box').css('display','none');
				},3000)
			},
			error: function(xhr, type, errorThrown) {
				//异常处理；
				// console.log(type);
			}
		});
	});
	//获取验证码
	document.getElementById("reg").addEventListener("tap", function() {
		var reg_phone = document.querySelector(".reg_phone").value;
		var reg1 = /(^1[3|4|5|7|8][0-9]{9}$)/;
		if(reg1.test(reg_phone) == false) {
			remind("请输入正确的手机号码");
			return
		}
		var reg_password = document.querySelector(".password").value;
		var reg2 = /^[0-9A-Za-z]{6,}$/;
		if(reg2.test(reg_password) == false) {
			remind("请输入6位以上密码");
			return
		}
		var pMobile = document.querySelector(".pMobile").value;
		var input_phone_message = document.querySelector("#input_phone_message").value;
		var wxUnionId = document.querySelector(".wxUnionId").value;
		mui.ajax("/api/v1/user/register", {
			data: {
				paramStr: "{'userName':'" + reg_phone + "','appVer':'h5','password':'" + reg_password + "','mobile':'" + reg_phone + "','pMobile':'" + pMobile + "','smsVerifyCode':'" + input_phone_message + "','sex':'M'"+",'wxUnionId':'"+wxUnionId+"'}"
			},
			dataType: 'json',
			type: 'post',
			success : function(data) {
				if(data.code == 200) {
					setTimeout(function() {
						mui.openWindow({
							url: 'http://wx.bshbsh.com/appdownload/',
						});
					}, 3000)
				}
				$('#hidden_box').css('display','block').text(data.message);
				setTimeout(function(){
					$('#hidden_box').css('display','none');
				},3000)
			},
			error : function(xhr , type , errorThrown) {
				//异常处理；
				alert("未知错误");
			}
		});
	});
});